package com.yxcz.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.yxcz.model.Supplier;
import com.yxcz.service.SupplierService;
import com.yxcz.util.ImportExcel;

@Controller
@RequestMapping("/Supplier")
public class SupplierController {

	@Resource
	private SupplierService supplierService;
	@Resource
	private ImportExcel importExcel;

	@RequestMapping("/index")
	public ModelAndView index(ModelAndView modelAndView) {

		modelAndView.addObject("suppliers", supplierService.list());
		modelAndView.setViewName("Supplier/index");
		return modelAndView;

	}

	/**
	 * 查看供应商资料
	 * 
	 * @param id
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping("/view/{id}")
	public ModelAndView view(@PathVariable("id") Integer id, ModelAndView modelAndView) {

		if (id == null) {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}

		modelAndView.addObject("supplier", supplierService.find(id));
		modelAndView.setViewName("Supplier/view");
		return modelAndView;

	}

	@RequestMapping("/delete")
	public ModelAndView delete(Integer id, ModelAndView modelAndView) {

		if (id == null) {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}

		supplierService.delete(id);
		modelAndView.addObject("msg", "删除");
		modelAndView.setViewName("Common/success");
		return modelAndView;

	}

	@RequestMapping("/add")
	public ModelAndView add(Supplier supplier, ModelAndView modelAndView) {

		try {
			supplierService.add(supplier);
		} catch (Exception e) {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}
		modelAndView.addObject("msg", "增加");
		modelAndView.setViewName("Common/success");
		return modelAndView;

	}

	@RequestMapping("/import")
	public ModelAndView _import(MultipartFile file, ModelAndView modelAndView, HttpServletRequest request) {

		String path = request.getSession().getServletContext().getRealPath("/resources/Upload/UserPhoto");

		String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."),
				file.getOriginalFilename().length());

		if (!(type.equals(".xlsx"))) {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}

		String fileName = new Date().getTime() + request.getSession().getAttribute("accountName").toString() + type;
		File targetFile = new File(path, fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}

		// 保存
		try {
			file.transferTo(targetFile);

		} catch (Exception e) {
			return null;
		}

		String[][] result = null;
		try {
			result = importExcel.getData(targetFile, 1);
		} catch (IOException e) {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}
		try {
			supplierService.addAll(result);
		} catch (Exception e) {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}
		
		modelAndView.addObject("msg", "导入");
		modelAndView.setViewName("Common/success");
		return modelAndView;

	}
	
	/**
	 * 修改
	 * 
	 * @param id
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable("id") Integer id, ModelAndView modelAndView) {

		if (id == null) {
			modelAndView.setViewName("redirect:/Common/500Page");
			return modelAndView;
		}

		modelAndView.addObject("supplier", supplierService.find(id));
		modelAndView.setViewName("Supplier/edit");
		return modelAndView;

	}
	
	@RequestMapping("/doEdit")
	public ModelAndView edit(Supplier supplier, ModelAndView modelAndView) {
		supplierService.update(supplier);
		modelAndView.addObject("msg", "保存");
		modelAndView.setViewName("Common/success");
		return modelAndView;


	}

}
